package com.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.mapper.ForumArticleMapper;
import com.forum.pojo.ForumArticle;
import com.forum.pojo.StudentInfo;
import com.forum.pojo.TeacherInfo;
import com.forum.pojo.vo.ArticleModify2;
import com.forum.pojo.vo.SearchTeacherVo;
import com.forum.result.Result;
import com.forum.result.ResultCodeEnum;
import com.forum.service.TeacherInfoService;
import com.forum.mapper.TeacherInfoMapper;
import com.forum.utils.JwtHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoMapper, TeacherInfo>
    implements TeacherInfoService{
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    TeacherInfoMapper teacherInfoMapper;
    @Autowired
    ForumArticleMapper forumArticleMapper;
    @Override
    public Result verify(String username, String password) {
        QueryWrapper<TeacherInfo> studentInfoQueryWrapper = new QueryWrapper<>();
        studentInfoQueryWrapper.eq("teacherId",username);
        if(teacherInfoMapper.selectOne(studentInfoQueryWrapper)==null){
            return  Result.build(null, ResultCodeEnum.USENAME);
        }
        studentInfoQueryWrapper.eq("password",password);
        if(teacherInfoMapper.selectOne(studentInfoQueryWrapper)==null){
            return  Result.build(null,ResultCodeEnum.PASSWORD);
        }
        TeacherInfo teacherInfo = teacherInfoMapper.selectOne(studentInfoQueryWrapper);
        String key="token:"+ UUID.randomUUID().toString();
        String teacher = JwtHelper.createToken(teacherInfo.getTeacherid(), "teacher");
        redisTemplate.opsForValue().set(key,teacher,48, TimeUnit.HOURS);
        System.out.println(key);
        return  Result.ok(key);
    }

    @Override
    public Result getPersonInfo(Long userId) {
        QueryWrapper<TeacherInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("teacherId", userId);
        return Result.ok(teacherInfoMapper.selectList(queryWrapper));
    }

    @Override
    public Page<TeacherInfo> getTeacherInfoByPage(SearchTeacherVo searchTeacherVo, Long currentPage, Long limit) {
        String major = searchTeacherVo.getMajor();
        String unit = searchTeacherVo.getUnit();
        QueryWrapper<TeacherInfo> teacherInfoQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(major))
        teacherInfoQueryWrapper.like("major",major);
        if(StringUtils.isNotEmpty(unit))
        teacherInfoQueryWrapper.like("unit",unit);
        Page<TeacherInfo> page = new Page<TeacherInfo>(currentPage,limit);
        Page<TeacherInfo> teacherInfoPage = teacherInfoMapper.selectPage(page, teacherInfoQueryWrapper);
        return teacherInfoPage;
    }

    @Override
    public Integer changeAdmin(Integer changedAdmin, String teacherId) {
            return teacherInfoMapper.changeAdmin(changedAdmin,teacherId);
    }

    @Override
    public Integer changeStatus(Integer changedStatus, String teacherId) {
        return teacherInfoMapper.changeStatus(changedStatus,teacherId);
    }

    @Override
    public Integer addTeacher(Long teacherId, String name, String unit, String occupation) {
        TeacherInfo teacherInfo = new TeacherInfo();
        teacherInfo.setName(name);
        teacherInfo.setTeacherid(teacherId);
        teacherInfo.setUnit(unit);
        teacherInfo.setOccupation(occupation);
        teacherInfo.setIdentity(1);
        teacherInfo.setIsAdmin(0);
        teacherInfo.setIllegalCount(0);
        teacherInfo.setIsFirstTimeLogin(1);
        teacherInfo.setNickname(name);
        long l = teacherId % 1000000;
        String password="zjut"+String.valueOf(l);
        teacherInfo.setPassword(password);
        teacherInfo.setStatus(0);
        return teacherInfoMapper.insert(teacherInfo);
    }

    @Override
    public List<ArticleModify2> getPersonArticleById(Long userId) {
        QueryWrapper<ForumArticle> queryWrapper=new QueryWrapper();
        queryWrapper.eq("teacherId",userId);
        List<ForumArticle> list = forumArticleMapper.selectList(queryWrapper);
        ArrayList<ArticleModify2> articleModify2s = new ArrayList<>();
        for(ForumArticle listItem:list){
            ArticleModify2 articleModify2 = new ArticleModify2();
            articleModify2.setForumArticle(listItem);
            Long Id = listItem.getUserId();
            String title = teacherInfoMapper.selectTitleById(Id);
            articleModify2.setTeacherTitle(title);
            articleModify2s.add(articleModify2);
        }
        return articleModify2s;
    }
}




